package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.lang.model.element.VariableElement;

import org.apache.commons.lang3.StringUtils;
import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateNameHelper;

import dbutil.DBHelper;
import entity.Download;
import entity.Software;

public class SoftDao {
	private boolean isSuccess;

	// 状态修改
	public boolean editSoftStatus(String status, String softName) throws SQLException {
		String sql = "update softwares set status = ? where softName = ?";
		Connection conn = null;
		PreparedStatement pstmt = null;

		try {
			conn = DBHelper.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, status);
			pstmt.setString(2, softName);
			int i = pstmt.executeUpdate();
			
			if(i > 0) {
				return true;
			}
			return false;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			pstmt.close();
		}
	}

	// 添加商品
	public boolean addSoftDownload(Software software) throws SQLException {
		StringBuffer sql = new StringBuffer("insert into softwares");
		sql.append(" (softName,softDesc,numlicense,assignedLicense,actMode,status,category,departLicense,");
		sql.append(" kmsServer,userLicense,usedLicense,createDate,keySeg)");
		sql.append(" values(?,?,?,?,?,?,?,?,?,?,?,?,?)");
		Connection conn = null;
		PreparedStatement pstmt = null;

		try {
			conn = DBHelper.getConnection();
			pstmt = conn.prepareStatement(sql.toString());
			pstmt.setString(1, software.getSoftName());
			pstmt.setString(2, software.getSoftDesc());
			pstmt.setLong(3, software.getNumLicense());
			pstmt.setLong(4, software.getAssignedLicense());
			pstmt.setString(5, software.getActMode());
			pstmt.setString(6, software.getStatus());
			pstmt.setString(7, software.getCategory());
			pstmt.setLong(8, software.getDepartLicense());
			pstmt.setString(9, software.getKmsServer());
			pstmt.setLong(10, software.getUserLicense());
			pstmt.setLong(11, software.getUsedLicense());
			pstmt.setDate(12, new java.sql.Date(software.getCreateDate().getTime()));
			pstmt.setString(13, software.getKeySeg());
			
			int i = pstmt.executeUpdate();
			if(i > 0) {
				return true;
			}
			return false;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			pstmt.close();
		}
	}

	// 查询商品
	public List<Software> querySofts(String softName, String secret) {
		StringBuffer sql = new StringBuffer("select * from softwares where 1=1");
				
		if(!StringUtils.isEmpty(softName)) {
			sql.append(" and softName like '%").append(softName).append("%'");
		}
		if(!StringUtils.isEmpty(secret)) {
			sql.append(" and keySeg like '%").append(secret).append("%'");
		}

		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Software soft = null;
		List<Software> list = null;

		try {
			conn = DBHelper.getConnection();
			pstmt = conn.prepareStatement(sql.toString());
			rs = pstmt.executeQuery();
			list = new ArrayList<Software>();

			while (rs.next()) {
				soft = new Software();
				soft.setActMode(rs.getString("actMode"));
				soft.setAssignedLicense(rs.getInt("assignedLicense"));
				soft.setCategory(rs.getString("category"));
				soft.setDepartLicense(rs.getInt("departLicense"));
				soft.setKmsServer(rs.getString("kmsServer"));
				soft.setNumLicense(rs.getInt("numLicense"));
				soft.setSoftDesc(rs.getString("softDesc"));
				soft.setSoftName(rs.getString("softName"));
				soft.setStatus(rs.getString("status"));
				soft.setUsedLicense(rs.getInt("usedLicense"));
				soft.setUserLicense(rs.getInt("userLicense"));
				soft.setKeySeg(rs.getString("keySeg"));
				soft.setCreateDate(rs.getDate("createDate"));
				
				list.add(soft);
			}
			return list;
			
		} catch (SQLException e) {
			e.printStackTrace();
			return list;
		}
	}

	// 查询应用
	public List<Download> queryDownloads() {
		String sql = "select * from downloads";
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Download download = null;
		List<Download> list = null;

		try {
			conn = DBHelper.getConnection();
			pstmt = conn.prepareStatement(sql.toString());
			rs = pstmt.executeQuery();
			list = new ArrayList<Download>();
			
			while (rs.next()) {
				download = new Download();
				download.setId(rs.getInt("id"));
				download.setAppName(rs.getString("appName"));
				download.setAppPicURL(rs.getString("appPicURL"));
				download.setAppLanguage(rs.getString("appLanguage"));
				download.setInner32(rs.getString("innerBit32URL"));
				download.setInner64(rs.getString("innerBit64URL"));
				download.setOuter32(rs.getString("outerBit32URL"));
				download.setOuter64(rs.getString("outerBit64URL"));
				download.setWebDesc(rs.getString("webDesc"));
				
				list.add(download);
			}
			return list;
			
		} catch (SQLException e) {
			e.printStackTrace();
			return list;
		}
		
	}

	// 添加应用
	public boolean addDownload(Download download) throws SQLException {
		StringBuffer sql = new StringBuffer("insert into downloads"); 
		sql.append(" (appName,appPicURL,appLanguage,");
		sql.append(" innerBit32URL,innerBit64URL,outerBit32URL,outerBit64URL,webDesc) ");
		sql.append(" values(?,?,?,?,?,?,?,?)");
		Connection conn = null;
		PreparedStatement pstmt = null;

		try {
			conn = DBHelper.getConnection();
			pstmt = conn.prepareStatement(sql.toString());

			pstmt.setString(1, download.getAppName());
			pstmt.setString(2, download.getAppPicURL());
			pstmt.setString(3, download.getAppLanguage());
			pstmt.setString(4, download.getInner32());
			pstmt.setString(5, download.getInner64());
			pstmt.setString(6, download.getOuter32());
			pstmt.setString(7, download.getOuter64());
			pstmt.setString(8, download.getWebDesc());

			int i = pstmt.executeUpdate();
			
			if(i > 0) {
				return true;
			}
			return false;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			pstmt.close();
		}
	}
	
	// 删除应用
	public boolean delDownload(String softName) throws SQLException {
		String sql = "delete from downloads where appName = ?";
		Connection conn = null;
		PreparedStatement pstmt = null;

		try {
			conn = DBHelper.getConnection();
			pstmt = conn.prepareStatement(sql.toString());

			pstmt.setString(1, softName);

			int i = pstmt.executeUpdate();
			
			if(i > 0) {
				return true;
			}
			return false;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			pstmt.close();
		}
	}
	
	// 编辑应用
	public boolean editDownload(Download download) throws SQLException {
		String sql = "update downloads set appLanguage=?,innerBit32URL=?,innerBit64URL=?,outerBit32URL=?,outerBit64URL=?,webDesc=?,appPicURL=? where appName=?";
		Connection conn = null;
		PreparedStatement pstmt = null;

		try {
			conn = DBHelper.getConnection();
			pstmt = conn.prepareStatement(sql);

			pstmt.setString(1, download.getAppLanguage());
			pstmt.setString(2, download.getInner32());
			pstmt.setString(3, download.getInner64());
			pstmt.setString(4, download.getOuter32());
			pstmt.setString(5, download.getOuter64());
			pstmt.setString(6, download.getWebDesc());
			pstmt.setString(7, download.getAppPicURL());
			pstmt.setString(8, download.getAppName());

			int i = pstmt.executeUpdate();

			if(i > 0) {
				return true;
			}

			return false;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			pstmt.close();
		}
	}
	
	// 删除软件
	public boolean delSoft(String softName) throws SQLException {
		String sql = "delete from softwares where softName=?";
		Connection conn = null;
		PreparedStatement pstmt = null;

		try {
			conn = DBHelper.getConnection();
			pstmt = conn.prepareStatement(sql);
			
			pstmt.setString(1, softName);
			
			int i = pstmt.executeUpdate();
			
			if(i > 0) {
				return true;
			}

			return false;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			pstmt.close();
		}
	}

	// 编辑软件状态
	public boolean editSoft(String numLic, String kmsS, String keySeg, String status, String softName) throws SQLException {
		String sql = "update softwares set numLicense=?,kmsServer=?,keySeg=?,status=? where softName=?";
		Connection conn = null;
		PreparedStatement pstmt = null;

		try {
			conn = DBHelper.getConnection();
			pstmt = conn.prepareStatement(sql);

			pstmt.setString(1, numLic);
			pstmt.setString(2, kmsS);
			pstmt.setString(3, keySeg);
			pstmt.setString(4, status);
			pstmt.setString(5, softName);

			int i = pstmt.executeUpdate();

			if(i > 0) {
				return true;
			}

			return false;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			pstmt.close();
		}
	}
	public SoftDao() {
	}

}
